package com.mbc.common.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CountryDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.data.CountryDataList;
import com.mbc.common.entities.Country;
import com.mbc.common.entities.CountryRpt;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.utils.KeyGenerator;

public class CountryMgr extends AbstractMgr {
	private CountryDAO countryDAO;
	private LogDAO logDAO;
	
	public CountryMgr() {
	}

	public CountryMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(Country entity) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			entity.setCountryId(KeyGenerator.generateKey(getConnection()));
			countryDAO.create(entity);

			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Country");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getCountryId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(Country entity) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			countryDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Country");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setKey(entity.getCountryId());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(long key,long userid) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Country");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setUserid(userid);
			l_logentity.setKey(key);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			countryDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public Country find(long key) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			return countryDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public ArrayList<Country> findAll() throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			return countryDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public CountryDataList findCountry(Country cri) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			return countryDAO.findCountry(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public Country findCountryByCode(Country entity) throws Exception {
		openConnection();
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());
			return countryDAO.findCountryByCode(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public JasperPrint prepareCountryReport(CountryRpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
					.getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
					countryDAO.prepareCountryReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/CountryRpt.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
